/**
 * @desc 日历选择
 * @author csch
 * @date 2009-03-20
 * @usage 在插入文本框时加上:onfocus="show_date(this,event);"
 */

var obj_text = null; //文本框对象

/**
 * @desc create_calendar_head:创建日历头部
 * @param h_year:默认年份 h_month:默认月份
 */
function create_calendar_head(h_year,h_month)
{
	h_month = h_month - 1;
	var start_date = 1900;
	var end_date = h_year + 50;	
	var j = 0;//用于循环的变量
	var s_year = "";//标识某年是否被选中
	var s_month = "";//标识某月是否被选中
	
	var str_head ="<table border='0' cellspacing='0' cellpadding='5'>\n";
	str_head += "<tr align='center' bgcolor='#D3E3FC'>\n<td width='206' nowrap>\n";

	//上一月
	str_head += "<input id='m_prior' type='button' onclick=\"change_month('prior');\" title='上一月' style='cursor:pointer;border:1px solid black;width:25px;height:20px;' value='&lt;'>&nbsp;";

	//输出年份下拉框
	str_head += "<select name='year' id='year' onchange='return change_calendar();'>\n";
	for(j = end_date ; j >= start_date ; j--)
	{
		s_year = (j == h_year) ? ' selected' : '';
		str_head += "<option value='" + j + "'" + s_year + ">" + j + "</option>\n";
	}
	str_head += "</select>年\n";
	
	//输出月份下拉框
	str_head += "<select name='month' id='month' onchange='return change_calendar();'>\n";
	for(j = 1 ; j <= 12 ; j++)
	{
		s_month = (j == h_month + 1) ? ' selected' : '';
		str_head += "<option value='" + j + "'" + s_month + ">" + j + "</option>\n";
	}
	str_head += "</select>月\n";
	//下一月
	str_head += "<input id='m_next' type='button' onclick=\"change_month('next');\" title='下一月' style='cursor:pointer;border:1px solid black;width:25px;height:20px;' value='&gt;'>";	
	str_head += "</td>\n</tr>\n</table>\n";
	return str_head;
}

/**
 * @desc 创建日期体部分
 * @param year:当前年 month:当前月
 */
function create_calendar_body(year,month)
{
	month = month - 1;//js中,0-11分别表示1-12月份
	var i = 0;
	var february = 0;	
	var tmp = 0; //临时存放日期
	var cur_s_year = document.getElementById('year').value + '-'; //当前选择的年份
	var cur_s_month = (document.getElementById('month').value < 10) ? ('0' + document.getElementById('month').value) : document.getElementById('month').value; //当前选择月份
	cur_s_month += '-';
	
	//每个月第一天是星期几,从0到6分别表示星期一到星期天
	var month_first_date = 0;
	
	/*
	 *  february:二月份的天数
	 *  若为闰年,则二月份为29天
	 *  若为平年,则二月份为28天
	 */	
	if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))  february = 29;
	else february = 28;
	
	
	//每月天数的数组
	var month_days = new Array(31,february,31,30,31,30,31,31,30,31,30,31);
	
	
	//星期数组
	var week = new Array("日","一","二","三","四","五","六");
	
	var str_calendar = "<table border='0' bgcolor='#FFFFFF' cellspacing='1' cellpadding='5'>\n<tr bgcolor='#E4ECF7' nowrap>\n";
	
	//输出日期的星期部分
	for(i = 0 ; i < 7 ; i++)
	{
		str_calendar += "<td width='20'>" + week[i] + "</td>\n";
	}
	str_calendar += "</tr>\n<tr bgcolor='#E4ECF7' nowrap>\n";
	
	//日期对象,用于获取所选月份的第一天是星期几
	var str_date = new Date(year,month,1);
	var month_first_date = str_date.getDay();
	//输出日期开始的空格部分
	for(i = 0 ; i < month_first_date ; i++)
	{
		str_calendar += "<td>&nbsp;</td>\n";
	}
	
	//获取选取月份的天数
	var days = month_days[month];
	//输出日期部分
	for(i = 1 ; i <= days ; i++)
	{
		tmp = (i < 10) ? ('0' + i) : i;
		str_calendar += "<td onclick=\"set_date('" + cur_s_year + cur_s_month + tmp + "');\" onmouseover=\"this.style.backgroundColor='#D3E3FC';this.style.cursor='pointer';\"";
		if(new Date().getDate() == i)
		{
			str_calendar += " bgcolor='orange' onmouseout=\"this.style.backgroundColor='orange';\"";
		}
		str_calendar += "  onmouseout=\"this.style.backgroundColor='#E4ECF7';\">";
		if(new Date(year,month,i).getDay() == 0 || new Date(year,month,i).getDay() == 6)
		{
			str_calendar += "<font color='#FF0000'>" + tmp + "</font>";
		}
		else
		{
			str_calendar += tmp;
		}
		str_calendar +="</td>\n";

		if(new Date(year,month,i).getDay() == 6)
		{
			str_calendar += "</tr>\n<tr bgcolor='#E4ECF7'>\n";
		}
	}
	
	//输出日期尾部空格部分
	var month_last_day = new Date(year,month,days).getDay();
	var end_spaces = 6 - month_last_day;
	for(i = 1 ; i <= end_spaces ; i++)
	{
		str_calendar += "<td>&nbsp;</td>\n";
	}
	str_calendar += "</tr>\n";

	//当天的日期
	var t_date = new Date();
	var t_year = t_date.getFullYear();
	var t_month = t_date.getMonth() + 1;
	t_month = (t_month < 10 ) ? ('0' + t_month) : t_month;
	var t_day = t_date.getDate();
	t_day = (t_day < 10) ? ('0' + t_day) : t_day;
	t_date = t_year + '-' + t_month + '-' + t_day;

	str_calendar += "<tr bgcolor='#D3E3FC' align='center'><td colspan='7' nowrap>";
	//上一年
	str_calendar += "<input id='y_prior' type='button' onclick=\"change_year('prior');\" title='上一年' style='cursor:pointer;border:1px solid black;width:40px;height:20px;' value='&lt;&lt;'>&nbsp;";
	str_calendar += "<input id='today' type='button' value='今天' onclick=\"set_date('" + t_date + "');\" style='border:1px solid black;width:40px;height:20px;'/>";
	str_calendar += "&nbsp;<input id='close' type='button' value='关闭' onclick=\"javasccript:document.getElementById('d_date').style.display='none';\" style='border:1px solid black;width:40px;height:20px;'/>";
	//下一年
	str_calendar += "&nbsp;<input id='y_next' type='button' onclick=\"change_year('next');\" title='下一年' style='cursor:pointer;border:1px solid black;width:40px;height:20px;' value='&gt;&gt;'>";
	str_calendar += "</td></tr></table>";
	return str_calendar;
}

/**
 * @desc 重新生成日期
 */
function change_calendar()
{
	var selected_year = document.getElementById('year').value;
	var selected_month = document.getElementById('month').value;
	document.getElementById('b_div').innerHTML = create_calendar_body(selected_year,selected_month);
}

/**
 * @desc 选择的年发生变化调用此方法
 * @param type:表示的是上一年还是一下年
 */
function change_year(type)
{
	var selected_year = 0;
	if(type == 'prior')
	{
		selected_year = parseInt(document.getElementById('year').value) - 1;
	}
	else if(type == 'next')
	{
		selected_year = parseInt(document.getElementById('year').value) + 1;
	}
	document.getElementById('year').value = selected_year;
	change_calendar();
}

/**
 * @desc 选择的月发生变化调用此方法
 * @param type:表示的是上一月还是一下月
 */
function change_month(type)
{
	var selected_month = 0;
	if(type == 'prior')
	{
		selected_month = parseInt(document.getElementById('month').value) - 1;
		if(selected_month == 0)
		{
			document.getElementById('year').value = parseInt(document.getElementById('year').value) - 1;
			selected_month = 12;
		}
	}
	else if(type == 'next')
	{
		selected_month = parseInt(document.getElementById('month').value) + 1;
		if(selected_month == 13)
		{
			document.getElementById('year').value = parseInt(document.getElementById('year').value) + 1;
			selected_month = 1;
		}
	}
	document.getElementById('month').value = selected_month;
	change_calendar();
}

/**
 * @desc 把选中的日期值赋值给文本框
 * @param date_value:选中的日期
 */
function set_date(date_value)
{
	obj_text.value = date_value;
	document.getElementById('d_date').style.display = 'none';
}

/**
 * @desc 日历初始化
 */
function init_calendar()
{
	var init_str = "<div id='d_date' style='display:none;top:100px;left:100px;position:absolute;z-index:1200;'>\n";
	init_str += "<div id='h_div'>\n";
	init_str += "h_csch";
	init_str += "</div>\n";
	init_str += "<div id='b_div'>\n";
	init_str += "b_csch";
	init_str += "</div>\n";
	init_str += "</div>\n";
	document.writeln(init_str);
}

/**
 * @desc 生成点击文本框时日历的坐标
 * @param obj:当前点击的对象
 */
function getXY(obj) {
	var x = obj.offsetLeft + 2;
	var y = obj.offsetTop + obj.offsetHeight + 2;
	while(obj = obj.offsetParent) {
		x += obj.offsetLeft;
		y += obj.offsetTop;
	}
	document.getElementById('d_date').style.left = x + 'px';
	document.getElementById('d_date').style.top = y + 'px';
}

/**
 * @desc 显示日期
 * @param text:文本框对象 event:为兼容IE外其它浏览器用
 */
function setday(text, event)
{
	var e = event || window.event;
	var obj = e.target || e.srcElement;
	var p_year = new Date().getFullYear();
	var p_month = new Date().getMonth() + 1;

	getXY(obj);
	obj_text = text;
	document.getElementById('h_div').innerHTML = create_calendar_head(p_year,p_month);
	document.getElementById('b_div').innerHTML = create_calendar_body(p_year,p_month);
	document.getElementById('d_date').style.display = "inline";
}
/**
 * @desc 点击页面其它地方时隐藏日历控件
 * @param event:为兼容IE外其它浏览器用
 */
document.onclick = function(event) {
    var e = event || window.event;
    var obj = e.target || e.srcElement;
    var obj_div = document.getElementById('d_date');
    if(obj_text != null) {
	    if((obj.id != obj_text.id || obj.name != obj_text.name) && obj.id != 'd_date' 
	       && obj.id != 'm_prior' && obj.id != 'm_next' && obj.id != 'y_prior' && obj.id != 'y_next' 
	       && obj.id != 'year' && obj.id != 'month' && obj.id != 'today' && obj.id != 'close' && obj.nodeName != 'OPTION') {
	      document.getElementById('d_date').style.display = 'none';
	    }
    }
}

init_calendar();